<?php 
$tpl=new TemplatePower("skin/sk_support.htm");
$tpl->prepare();
//---------- config
$manager="H&#7894; TR&#7906; TR&#7920;C TUY&#7870;N";// tieu de trang

$par_page		="sr_support";// vi tri trang hien thi trong site
$item_name		="nick support";
$item           ="yahoo";
$id_item        ="id_yahoo";

$itemname 		="nicks";
if($_GET['p'])
{
	$p=intval($_GET['p']);
	if($p==0){
		$p=1;
	}
}else {
	$p=1;
}
$id=intval($_GET['id']);
$tpl->assignGlobal("item_name",$item_name);
$tpl->assignGlobal("p",$p);
$tpl->assignGlobal("manager",$manager);
$tpl->assignGlobal("par_page",$par_page);

if($_GET['code']=='00' || !$_GET['code']){
	showlist();	
}
if($_GET['code']=='01'){
	$tpl->newBlock("addnew_item");
	$tpl->assign("thu_tu",ordering($item));
	showlist();
}
if($_GET['code']=='02'){
	$in_name=compile_post('name');
	if ($in_name)
	{
		$a=array(
					'name'=>$in_name,
				);
		$a['thu_tu']=intval(compile_post('thu_tu'));
		$a['active']=intval(compile_post('active'));
		$a['sky']=intval(compile_post('sky'));
		
		$a['nic']=compile_post('nic');
		$a['info']=compile_post('info');
		$b=$DB->compile_db_insert_string($a);
		$sql="INSERT INTO ".$item." (".$b['FIELD_NAMES'].") VALUES (".$b['FIELD_VALUES'].")";
		$DB->query($sql);
		message("Th&#234;m m&#7899;i th&#224;nh c&#244;ng !");
	}
	else
	{
		message("Kh&#244;ng c&#243; d&#7919; li&#7879;u &#273;&#7847;u v&#224;o ! H&#227;y th&#7917; l&#7841;i !");
	}
	showlist();
}
if($_GET['code']=='03'){
	$tpl->newBlock("update_item");
	if($id){
		$sql="SELECT * FROM ".$item." WHERE ".$id_item."=".$id;
		$db=$DB->query($sql);
		if($rs=mysql_fetch_array($db)){
			$tpl->assign("name",$rs['name']);
			$tpl->assign("info",$rs['info']);
			$tpl->assign("nic",$rs['nic']);
			$tpl->assign("id",$rs[$id_item]);
			$tpl->assign("thu_tu",$rs['thu_tu']);
			if($rs['active']==1){
				$tpl->assign("active","checked");
			}else {
				$tpl->assign("active","");
			}
			if($rs['sky']==1){
				$tpl->assign("sky","checked");
			}else {
				$tpl->assign("yahoo","checked");
			}
		}
	}
	showlist();
}
if($_GET['code']=='04'){

	$id=intval($_GET['id']);
	if ($id)
	{
		$in_name=compile_post('name');
		if ($in_name)
		{
			$a=array(
						'name'=>$in_name,
					);
			$a['sky']=intval(compile_post('sky'));
			$a['thu_tu']=intval(compile_post('thu_tu'));
			$a['nic']=compile_post('nic');
			$a['info']=compile_post('info');
			
			$a['active']=intval(compile_post('active'));
			$b=$DB->compile_db_update_string($a);
			$sql="UPDATE ".$item." SET ".$b." WHERE ".$id_item."=".$id;
			$DB->query($sql);
			message("S&#7917;a th&#244;ng tin th&#224;nh c&#244;ng !");
		}
		else
		{
			message("Kh&#244;ng c&#243; d&#7919; li&#7879;u &#273;&#7847;u v&#224;o ! H&#227;y th&#7917; l&#7841;i !");
		}
	}
	showlist();
}
if($_GET['code']=='05'){
	$sql="DELETE FROM ".$item." WHERE ".$id_item."=".$id;
	$DB->query($sql);
	message("&#272;&#227; x&#243;a xong !");
	showlist();
}
if($_GET['code']=='07'){
	$id_items=$_POST['delallitem'];
	if($id_items);
	$list_id=0;
	foreach($id_items as $idit){
		$list_id.=','.$idit;
	}
	$sql="DELETE FROM ".$item." WHERE ".$id_item." IN(".$list_id.")";
	$DB->query($sql);
	message("&#272;&#227; x&#243;a xong !");
	showlist();
}
if($_GET['code']=='06'){
	$sql="SELECT * FROM ".$item." ORDER BY thu_tu DESC, name ASC";
	$db=phan_trang($p,"?page=".$par_page."&code=00",$sql,$maxpage,$maxitem,'p');
	change_active_order_paging($item,$id_item,$db['db']);
	
	showlist();
}
function showlist(){
	global $DB, $p, $maxitem, $maxpage, $tpl, $item, $id_item;
	$sql="SELECT * FROM ".$item." ORDER BY thu_tu DESC, name ASC";
	$db=phan_trang($p,"?page=".$par_page."&code=00",$sql,$maxpage,$maxitem,'p');
	$tt=0;
	$i=0;
	$tpl->newBlock("show_item");
	while($rs=mysql_fetch_array($db['db'])){
		$tt++;
		$tpl->newBlock("list_item");
		$tpl->assign("name",$rs['name']);
		$tpl->assign("thu_tu",$rs['thu_tu']);
		$tpl->assign("nic",$rs['nic']);
		$tpl->assign("id",$rs[$id_item]);
		$tpl->assign("stt",$tt);
		if($rs['active']==1){
			$tpl->assign("active","checked");
			$tpl->assign("name",$rs['name']);
		}else {
			$tpl->assign("active","");
			$tpl->assign("name","<font color='red'>".$rs['name']."</font>");
		}
		$tpl->assign("link_edit","?page=sr_support&code=03&id=".$rs[$id_item]);
		if($rs['sky']==1){
			$tpl->assign("type","Sky");
		}else {
			$tpl->assign("type","Yahoo");
		}
		
	}
}
$tpl->printToScreen();
?>